<?php

namespace App\Models\system;

use App\Models\BaseModel;
use Carbon\Carbon;

class NewsModel extends BaseModel
{
    protected $table = 'news';
    protected $primaryKey = 'id';

    const PLATFORM_ACTIVITIES = 1;
    const PRODUCT_NEW = 2;

    public static function getClassName($class_id) {
        switch ($class_id) {
            case self::PLATFORM_ACTIVITIES:
                return '平台活动';
            case self::PRODUCT_NEW:
                return '新产品上线';
            default:
                return '未知活动';
        }
    }


    public function getActivityTimeAttribute($value)
    {
        return $value ? Carbon::createFromTimestamp($value)->format('Y-m-d') : '';
    }


    public function setActivityTimeAttribute($value)
    {
        $this->attributes['activity_time'] = $value ? strtotime($value) : '';
    }

    public function getClassIdAttribute($value)
    {
        return $value ? self::getClassName($value) : '';
    }


    public function newsBrowse(): \Illuminate\Database\Eloquent\Relations\HasOne
    {
        return $this->hasOne(NewsMiddleModel::class, 'news_id', 'id');
    }

}
